package com.fenghongzhang.day006;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.VideoView;

import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.functions.Consumer;

public class MainActivity extends AppCompatActivity {


    RxPermissions rxPermissions = new RxPermissions(this);
    private Button btn;
    private VideoView video;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        btn = (Button) findViewById(R.id.btn);
        video = (VideoView) findViewById(R.id.video);



        //6.0 -> 23
        //8.0
        rxPermissions.request(Manifest.permission.CAMERA
                ,Manifest.permission.RECORD_AUDIO
                ,Manifest.permission.READ_EXTERNAL_STORAGE
                ,Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                Toast.makeText(MainActivity.this, ""+aBoolean, Toast.LENGTH_SHORT).show();
            }
        });

//        rxPermissions.requestEach(Manifest.permission.CAMERA,Manifest.permission.RECORD_AUDIO)
//                .subscribe(new Consumer<Permission>() {
//                    @Override
//                    public void accept(Permission permission) throws Exception {
//                        if(permission.granted){
//                            Toast.makeText(MainActivity.this, "同意", Toast.LENGTH_SHORT).show();
//                        }else if(permission.shouldShowRequestPermissionRationale){
//                            Toast.makeText(MainActivity.this, "不同意", Toast.LENGTH_SHORT).show();
//                        }else{
//                            Toast.makeText(MainActivity.this, "一致不同意", Toast.LENGTH_SHORT).show();
//                        }
//                    }
//                });


    }


    public void record(View view) {
        Intent intent = new Intent();
        intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
        startActivityForResult(intent,101);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //sd
        if (requestCode == 101 && resultCode == Activity.RESULT_OK) {//获得录制的视频
            Uri uri = data.getData();
            video.setVideoURI(uri);
            video.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    video.start();
                }
            });
        }
    }
}
